from django.contrib import admin

# Register your models here.
from django.utils.html import format_html

from .models import Customer

admin.site.site_title = "MyDjango管理后台"
admin.site.site_header = "MyDjango"


@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):

    def change_button(self, obj):
        return format_html('<a class="btn" href="/admin/my_app/my_model/{}/change/">修改</a>', obj.id)

    def delete_button(self, obj):
        return format_html('<a class="btn" href="/admin/my_app/my_model/{}/delete/">Delete</a>', obj.id)

    # fieldsets = ('action', {"fields": ('change_button', 'delete_button')})
    # 定制显示的列
    list_display = ('name', 'phonenumber', 'qq', 'address')
    # 定制列可以跳转
    list_display_links = ('name', 'phonenumber')
    # 定制 模糊搜索字段
    search_fields = ('name',)
    # 完全匹配搜索
    # search_fields = ('name__exact',)
    # 列表时，连表查询是否自动select_related
    # list_select_related = ('name',)
    # 定制搜索-下拉列表\
    # list_filter = ('name',)
    # 列表时，可以编辑的列
    # list_editable = ('qq',)
    # 列表时，对Date和DateTime类型进行搜索
    # date_hierarchy = 'publishDate'
    # 列表时，数据排序规则
    ordering = ('-id',)
    # ordering = ('-id',)  # 倒序
    # 详细页面，针对FK和M2M字段变成以Input框形式
    # raw_id_fields = ('publish', 'authors',)
    # 详细页面时，显示字段的字段
    fields = ('name', 'phonenumber', 'qq', 'address')

    # 详细页面时，排除的字段
    # exclude = ('is_del',)
    # 详细页面时，只读字段
    # readonly_fields = ('name',)
    # 可新增，不可编辑字段
    def get_readonly_fields(self, request, obj=None):
        return ('name',) if obj else ()
        # if obj:
        #     readonly_fields = ('name',)
        # else:
        #     readonly_fields = ()
        # return readonly_fields

    # 详细页面时，多对多显示时，数据移动选择（方向：上下和左右）
    # filter_vertical = ('address',)
    # 详细页面时，使用radio显示选项（外键默认使用select）
    # radio_fields = {"publish": admin.VERTICAL}
    actions = ['show_selected', 'delete_selected']

# inlines，详细页面，如果有其他表和当前表做外键，那么详细页面可以进行动态增加和删除
# class BookInline(admin.StackedInline):  # TabularInline
#     extra = 0
#     model = models.Book
#
# class GroupAdminMode(admin.ModelAdmin):
#     list_display = ('id', 'title',)
#     inlines = [BookInline, ]
# 列表时，定制action中的操作
# @admin.register(models.Book)
# class BookAdmin(admin.ModelAdmin):
#
#     # 定制Action行为具体方法
#     def show(self, request, queryset):
#         print(self, request, queryset)
#         print(request.POST.getlist('_selected_action'))
#
#     func.short_description = "中文显示自定义Actions"
#     actions = [show, ]
#
#     # Action选项都是在页面上方显示
#     actions_on_top = True
#     # Action选项都是在页面下方显示
#     actions_on_bottom = False
#
#     # 是否显示选择个数
#     actions_selection_counter = True
# admin.site.register(Customer, CustomerAdmin)
# 详细页面时，使用fieldsets标签对数据进行分割显示
# @admin.register(models.Book)
# class BookAdmin(admin.ModelAdmin):
#     fieldsets = (
#         ('基本数据', {
#             'fields': ('title', 'price')
#         }),
#         ('其他', {
#             'classes': ('collapse', 'wide', 'extrapretty'),  # 'collapse','wide', 'extrapretty'
#             'fields': ('title', 'price'), 'classes': ['collapse']  # 设置为可折叠,
#         }),
#     )
# form = ModelForm，用于定制用户请求时候表单验证
# from mysite import models
# from django.forms import ModelForm
# from django.forms import fields
#
#
# class MyForm(ModelForm):
#     others = fields.CharField()
#
#     class Meta:
#         model = models = models.Book
#         fields = "__all__"
#
#
# @admin.register(models.Book)
# class BookAdmin(admin.ModelAdmin):
#     form = MyForm

# empty_value_display = "列数据为空时，显示默认值"
# @admin.register(models.Book)
# class BookAdmin(admin.ModelAdmin):
#     empty_value_display = "列数据为空时，默认显示"
#
#     list_display = ('title', 'price', 'publish')
#
#     def up(self, obj):
#         return obj.publish
#
#     up.empty_value_display = "指定列数据为空时，默认显示"

# 虚拟字段
# models.py:
# class Poll(models.Model):
#     ......
#
#     def was_published_recently(self):  # 通过函数定义虚拟字段
#         return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
#
#     was_published_recently.admin_order_field = 'pub_date'  # 指定排序字段
#     was_published_recently.boolean = True  # 如果指定了这个字段显示的时候体现为图表，比较美观
#     was_published_recently.short_description = 'Published recently?'  # 重定义字段显示名称,但是非数字段似乎不生效
#
#
# # admin.py:
# class PollAdmin(admin.ModelAdmin):
#     list_display = ('question', 'pub_date', 'was_published_recently')
